﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using TruyenCuoiViet.Resources;

namespace TruyenCuoiViet.ViewModels
{
    public class MainViewModel : INotifyPropertyChanged
    {
        public static string HomeAdress = "http://www.truyencuoihay.vn";
        public MainViewModel()
        {
            this.Categories = new ObservableCollection<CategoryViewModel>();
        }

        /// <summary>
        /// A collection for ItemViewModel objects.
        /// </summary>
        public ObservableCollection<CategoryViewModel> Categories { get; private set; }

        /// <summary>
        /// Sample property that returns a localized string
        /// </summary>
        public string LocalizedSampleProperty
        {
            get
            {
                return AppResources.SampleProperty;
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates and adds a few ItemViewModel objects into the Items collection.
        /// </summary>
        public void LoadData(int categoryId)
        {
            if (!IsDataLoaded)
            {
                Categories.Add(new CategoryViewModel { Id = 1, Name = "Mới nhất", Page = 1, Url = "" });
                Categories.Add(new CategoryViewModel { Id = 2, Name = "Truyện cười vova", Page = 1, Url = "/truyen-cuoi-vova" });
                Categories.Add(new CategoryViewModel { Id = 3, Name = "Truyện cười tình yêu", Page = 1, Url = "/truyen-cuoi-tinh-yeu" });
                Categories.Add(new CategoryViewModel { Id = 4, Name = "Truyện cười dân gian", Page = 1, Url = "/truyen-cuoi-dan-gian" });
                Categories.Add(new CategoryViewModel { Id = 5, Name = "Truyện cười khác", Page = 1, Url = "/truyen-cuoi-khac" });
                Categories.Add(new CategoryViewModel { Id = 6, Name = "TÌm kiếm", Page = 1, Url = "/search" });
                NotifyPropertyChanged("Categories");
                this.IsDataLoaded = true;
            }
            
            foreach (CategoryViewModel c in Categories)
            {
                if (c.Id == categoryId)
                {
                    c.Loaddata();
                    break;
                }
            }

        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public CategoryViewModel FindCategory(int Id)
        {
            foreach (CategoryViewModel v in Categories)
            {
                if (v.Id == Id)
                    return v;
            }
            return null;
        }

    }
}